package lesson6;

import cn.hutool.core.thread.ThreadFactoryBuilder;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

public class MyForkJoin extends RecursiveTask<Integer> {

    int n;

    MyForkJoin(int n){
        this.n = n;
    }


    @Override
    protected Integer compute() {
        if(n <= 1){
            return n;
        }

        MyForkJoin m1 = new MyForkJoin(n-1);
        m1.fork();

        MyForkJoin m2 = new MyForkJoin(n-2);
        m2.fork();

        return m1.join() + m2.join();
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
        ForkJoinTask task = forkJoinPool.submit(new MyForkJoin(50));
        System.out.println(task.get());
        /*for(int i = 0; i < 10; i++){
            ForkJoinTask task = forkJoinPool.submit(new MyForkJoin(i));
            System.out.println(task.get());
        }*/


    }
}
